package com.example.chapter09;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBUtil {
    // 假设使用MySQL数据库，这里填写你的MySQL驱动名、连接地址、用户名和密码
    private static final String drivername = "com.mysql.cj.jdbc.Driver";
    private static final String conUrl = "jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC";
    private static final String username = "your_username";
    private static final String password = "your_password";

    public static Connection getCon() {
        try {
            Class.forName(drivername);
            Connection con = DriverManager.getConnection(conUrl, username, password);
            return con;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void release(ResultSet rs, Statement stmt, Connection con) {
        if (rs!= null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (stmt!= null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (con!= null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}